﻿using LxCFD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;


namespace LxCFD
{
    public partial class MatrixADbBase : ScalarBase
    {
        protected List<int> e_FixValue = new List<int>();
        protected List<int> e_ALUO0 = new List<int>();
        protected List<int> e_Couple = new List<int>();

        public double[] ADiag;//线性方程组对角线
        public double[] b;//线性方程组 b

        public ValueBase<double> Source;

        public double RelaxFactor = 1;
        public override void CheckCreate(FvZoneBase zone)
        {
            base.CheckCreate(zone);
            LxTool.CheckCreateArray(ref b, Cells.Length);
            LxTool.CheckCreateArray(ref ADiag, Cells.Length);
        }
        protected void clear_eFlags()
        {
            e_FixValue.Clear();
            e_ALUO0.Clear();
            e_Couple.Clear();
        }
        #region Relax
        public virtual void Relax(int taskIndex)
        {
            if (RelaxFactor == 1) return;
            relax(InnerIndex, taskIndex);
        }
        protected void relax(IndexNumber indexNumber, int taskIndex)
        {
            double relax1 = (1 - RelaxFactor);
            double relaxFactorReverse = 1 / RelaxFactor;
            indexNumber.CellIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                ref double adiag = ref ADiag[i1];
                adiag *= relaxFactorReverse;
                b[i1] += relax1 * adiag * x[i1];
            }
        }
        protected void relax_bPart(IndexNumber indexNumber, int taskIndex)//当A不变时，relax不需要重复求A，仅求b减少计算量
        {
            double relax1 = (1 - RelaxFactor);
            indexNumber.CellIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                b[i1] += relax1 * ADiag[i1] * x[i1];
            }
        }

        #endregion
    }
}
